package com.client.lu;


import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.Vector;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.IntPoint;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.junit.Test;

import com.client.lucene.ik.IKAnalyzer6y;



public class CreateIndex {
	
	
	@Test
	public void createIndex() throws Exception{
		Path indexPath=Paths.get("index");
		
		Directory dir=FSDirectory.open(indexPath);
		
		Analyzer analyzer=new IKAnalyzer6y();
		
		IndexWriterConfig config=
				new IndexWriterConfig(analyzer);
		config.setOpenMode(OpenMode.CREATE_OR_APPEND);
		
		
	FileInputStream in1 = 
				new FileInputStream
				("C:\\Users\\Administrator\\Documents\\Tencent Files\\FileRecv\\mgck2017\\色情词库.txt");
		Vector<InputStream> v = new Vector<>();
		v.add(in1);
	
				Enumeration<InputStream> e = v.elements();

				SequenceInputStream sis = new SequenceInputStream(e);
				byte[] bs=new byte[10000];
				int len=sis.read(bs);
				String a=new String(bs,0,len);
				String[] ss=a.split("\n");
				System.out.println(ss[0]);
				
				
				sis.close();
				Document doc3=new Document();
				
				for (String string : ss) {
					System.out.println(string);
					doc3.add(new TextField("content",string,Store.YES));
					
					
					
				}
		
		
		
		
		
		IndexWriter writer=new IndexWriter(dir,config);
		
	    writer.addDocument(doc3);
		writer.commit();
}
}